<script setup>
import { ref, defineEmits } from 'vue'
import { ElMessage } from 'element-plus'
import { updateByIdApi } from '@/api/warehousingentry/warehousingEntryDetailsSort'

const emit = defineEmits(['refresh'])
const ruleFormRef = ref(null)
const rules = ref({
  count: [
    { required: true, message: '请输入分拣数量', trigger: 'blur' }
  ]
})

const dialogVisible = ref(false)
const dialogFormData = ref({
  id: null,
  count: '',
})

const resetForm = () => {
  dialogVisible.value = false
  dialogFormData.value = {
    id: null,
    count: '',
  }
  ruleFormRef.value?.resetFields()
}

const confirm = async () => {
  if (!ruleFormRef.value) return
  await ruleFormRef.value.validate(async (valid, fields) => {
    if (valid) {
      let res = null
      res = await updateByIdApi(dialogFormData.value)
      if (res.code) {
        ElMessage.success('操作成功!')
        resetForm()
        emit('refresh')
      } else {
        ElMessage.error('操作失败！')
      }
    } else {
      console.log('error submit!', fields)
    }
  })
}

defineExpose({
  dialogFormData,
  dialogVisible
})
</script>

<template>
  <el-dialog v-model="dialogVisible" title="分拣信息" width="500px">
    <el-form :model="dialogFormData" label-width="120px" ref="ruleFormRef" :rules="rules">
      <el-form-item label="分拣数量" prop="count">
        <el-input-number v-model="dialogFormData.count" :min="0" />
      </el-form-item>
      <!-- <el-form-item label="过期时间" prop="">
        <el-date-picker v-model="dialogFormData.predictedArrivalTime" type="datetime" placeholder="请选择预测到达时间"
          style="width: 100%" />
      </el-form-item> -->
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="resetForm">取消</el-button>
        <el-button type="primary" @click="confirm">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>